#!/bin/bash

# JDK版本切换脚本
echo "🔧 JDK版本切换工具"
echo "=============================="

# 颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# 显示当前版本
echo -e "${YELLOW}当前Java版本:${NC}"
java -version 2>&1 | head -n 1

# 显示可用版本
echo -e "${GREEN}可用Java版本:${NC}"
echo "1) Java 8   - /Users/fish/Library/Java/JavaVirtualMachines/corretto-1.8.0_412/Contents/Home"
echo "2) Java 11  - /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home"
echo "3) Java 17  - /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
echo "4) Java 24  - /Library/Java/JavaVirtualMachines/jdk-24.jdk/Contents/Home"

# 获取用户选择
read -p "请选择要切换的Java版本 (1-4): " choice

case $choice in
    1)
        export JAVA_HOME="/Users/fish/Library/Java/JavaVirtualMachines/corretto-1.8.0_412/Contents/Home"
        ;;
    2)
        export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home"
        ;;
    3)
        export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
        ;;
    4)
        export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-24.jdk/Contents/Home"
        ;;
    *)
        echo -e "${RED}无效选择，取消操作${NC}"
        exit 1
        ;;
esac

# 更新PATH
export PATH="$JAVA_HOME/bin:$PATH"

# 添加到shell配置文件
echo "export JAVA_HOME=$JAVA_HOME" >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc

# 显示切换结果
echo -e "${GREEN}✅ Java版本已切换:${NC}"
java -version 2>&1 | head -n 1
echo "JAVA_HOME=$JAVA_HOME"

# 验证Maven配置
echo -e "${YELLOW}Maven Java版本:${NC}"
mvn -version 2>&1 | grep "Java version"

echo -e "${GREEN}✅ 切换完成！请重新打开终端或运行: source ~/.zshrc${NC}"